package com.app.util.beanutil;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.beanutils.BeanMap;
import org.apache.commons.beanutils.BeanUtils;

import com.app.util.common.ObjUtil;



public class BeanUtil {

	/**
	 * 将一个对象复制到另一个对像
	 * @param dest
	 * @param orig
	 */
	public  static void copyBean(Object source,Object dest){
		try{
			org.springframework.beans.BeanUtils.copyProperties(source, dest);
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	
	
	
	
	
	/**
	 * 将request属性，对应放入到对象
	 * @param dest
	 * @param request
	 */
	public  static void copyProperty(Object dest,HttpServletRequest request){
		try {
			BeanUtilBean.class.newInstance().copyProperty(dest, request,null);
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	/**
	 * 将request属性，对应放入到对象
	 * @param ignoreArr 忽略的属性
	 * @param dest
	 * @param request
	 */
	public  static void copyProperty(Object dest,HttpServletRequest request,String[] ignoreArr){
		try {
			BeanUtilBean.class.newInstance().copyProperty(dest, request,ignoreArr);
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public  static void populate(Object bean,Map properties){
		try {
			BeanUtils.populate(bean, properties);
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 对象转化成map
	 * @param obj
	 * @return
	 */
	public static Map<String, Object> objectToMap(Object obj) {  
        if(obj == null)  
            return null;   
        
        Map<String,Object> map = new HashMap<String,Object>();
        try{
	        BeanMap beanMap = new org.apache.commons.beanutils.BeanMap(obj); 
	        for(Entry<Object,Object> entry:beanMap.entrySet()){
	        	Object key = entry.getKey();
	        	Object value = entry.getValue();
	        	map.put((String)key, value);
	        }
        }catch(Exception e){
        	e.printStackTrace();
        }
        return map;
    }   

	/**
	 * 对象转化成map
	 * @param obj
	 * ignores,排除的属性
	 * @return
	 */
	public static Map<String, Object> objectToMap(Object obj,String[] ignores) {  
        if(obj == null)  
            return null;   
        
        Map<String,Object> map = new HashMap<String,Object>();
        try{
	        BeanMap beanMap = new org.apache.commons.beanutils.BeanMap(obj); 
	        for(Entry<Object,Object> entry:beanMap.entrySet()){
	        	String key = (String)entry.getKey();
	        	Object value = entry.getValue();
	        	
	        	if(ignores != null){
	        		boolean flag = false;
	        		for(String str:ignores){
	        			if(str.equals(key)){
	        				flag = true;
	        				break;
	        			}
	        		}
	        		if(flag){
	        			continue;
	        		}
	        	}
	        	map.put(key, value);
	        }
        }catch(Exception e){
        	e.printStackTrace();
        }
        return map;
    }  
	
	public static Map<String,String> mapToStrMap(Map<String,Object> map){
		Map<String,String> destMap = new HashMap<String,String>();
		for(Entry<String,Object> entry: map.entrySet()) {
			destMap.put(entry.getKey(), ObjUtil.objToStr(entry.getValue()));
		}
		return destMap;
	}
	
	
	public  static void copyBean(Object source,Object dest,String[] ignoreArr){
		try{
			org.springframework.beans.BeanUtils.copyProperties(source, dest,ignoreArr);
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	
}
